package admin

import (
	"context"

	"server-go/api/reward/internal/svc"
	"server-go/api/reward/internal/types"
	"server-go/api/reward/internal/util"
	"server-go/rpc/reward/types/reward"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetDataLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDataLogic {
	return &GetDataLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetDataLogic) GetData(req *types.AdminGetDataInput) (resp *types.AdminGetDataOutput, err error) {
	out := &types.AdminGetDataOutput{Status: 400}
	if !util.IsAdmin(l.ctx) {
		return out, nil
	}
	res, err := l.svcCtx.RewardRpc.AdminGetData(l.ctx, &reward.AdminGetDataInput{FormNum: req.FormNum, IsLogin: req.IsLogin})
	if err != nil {
		return out, err
	}
	out.Status = res.Status
	for _, v := range res.Data {
		out.Data = append(out.Data, &types.AdminGetData{
			Key:       v.Key,
			Bounty:    v.Bounty,
			Email:     v.Email,
			EndDate:   v.Enddate,
			Audit:     v.Audit,
			NickName:  v.Nickname,
			StartDate: v.Startdate,
			TaskIntro: v.Taskintro,
			TaskTitle: v.Tasktitle,
			TypeName:  v.TypeName,
		})
	}
	return out, nil
}
